//获取应用实例
const app = getApp();
// 引入AES.js
var baseJs = require("../../common/base.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    bg: "../../images/loginBg.jpg",
    uid: '',
    time: null,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      uid: wx.getStorageSync('uid'),
    })
  },

  // 登录
  formSubmit: function (e) {
    var that = this;
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: baseJs.api + baseJs.encryUrl("method=OrdinaryUserLogin&UserName=" + e.detail.value.phone + "&PassWord=" + e.detail.value.password),
      success: function (res) {
        var json = JSON.parse(baseJs.decryUrl(res.data));
        wx.hideLoading();
        if (json.ResultFlag == 1) {
          // 同步方式存储表单数据
          console.log(json);
          wx.setStorageSync('uid', json.ResultData.Id);
          wx.setStorageSync('userType', json.ResultData.UserType);
          wx.showToast({
            title: json.ResultMsg,
            icon: 'none',
            duration: 1000
          });
          wx.login({
            success(res) {
              //js调用登陆命令获取到code
              // console.log(res);
              var codeString = res.code
              if (res.code) {
                wx.request({ //获取openid
                  url: baseJs.api + baseJs.encryUrl("method=GetSmallProgramOpenIdFromUserCode&Code=" + codeString + "&UID=" + json.ResultData.Id),
                  success: function (res) {
                    var resultData = JSON.parse(baseJs.decryUrl(res.data));
                    console.log(resultData);
                    that.setData({
                      userInfo: app.globalData.userInfo,
                    })
                    // 将openid存入
                    wx.setStorageSync('openID', resultData.ResultData)
                  }
                })
                // that.getAccessToken()
              }
              else {
                console.log('登录失败！' + res.errMsg)
              }
            }
          })

          that.GetUserInfoByIDs();
          //获取订阅权限
          // var tmplIds = ['jfaQsjZRgn3nhewyN-jQGs95jGHKlCMvjPs4mx_dIMU', '-qZkmzvBU9_9OypcJYz11eiin5rDfCCq6JGGLcCM2-4', 'z3NLD_OWmR2BZzCK2QFuZc-MXDPe0jxAz2VL8tB35LM']
          // var param = { tmplIds }
          // app.checkSub(param).then(res => { })
          setTimeout(function () {
            // console.log(getCurrentPages());
            if (getCurrentPages().length >= 2) {
              wx.navigateBack()
            } else {
              wx.reLaunch({
                url: '/pages/mine/mine',
              })
            }
            // 

          }, 500)
        } else {
          wx.showToast({
            title: json.ResultMsg,
            icon: 'none',
            duration: 2000
          });
        }

      }
    })
  },

  //获取用户信息
  GetUserInfoByIDs: function () {
    var that = this;
    var uid = wx.getStorageSync('uid');
    wx.request({
      url: baseJs.api + baseJs.encryUrl("method=GetUserInfoByIDs&UID=" + uid),
      success: function (res) {
        console.log(JSON.parse(baseJs.decryUrl(res.data)));
        var json = JSON.parse(baseJs.decryUrl(res.data));
        var BusinessIsOpenChuChuang = json.ResultData.BusinessIsOpenChuChuang;
        var BusinessIsFinishEnter = json.ResultData.BusinessIsFinishEnter;
        if (json.ResultFlag == 1) {
          that.setData({
            BusinessIsOpenChuChuang: BusinessIsOpenChuChuang,
            BusinessIsFinishEnter: BusinessIsFinishEnter,
          })
          wx.setStorageSync('BusinessIsOpenChuChuang', json.ResultData.BusinessIsOpenChuChuang);
          wx.setStorageSync('BusinessIsFinishEnter', json.ResultData.BusinessIsFinishEnter);
          wx.setStorageSync('BusinessAllName', json.ResultData.BusinessAllName);
          app.globalData.IsAgentHeHuoRen=json.ResultData.IsAgentHeHuoRen //用于判断数据总览
          app.globalData.IsPartner=json.ResultData.IsPartner
          app.globalData.IsAgentBigAreaManager=json.ResultData.IsAgentBigAreaManager
        }
      }
    })
  },

  // 获得access_token
  // getAccessToken() {
  //   if (wx.getStorageSync('uid')) {
  //     var that = this
  //     clearInterval(that.data.time)
  //     var appId = 'wx0aaf68f9500537d1';
  //     wx.request({
  //       url: baseJs.api_Utils + 'GetWXSmallAppSecret', //获取appsecret
  //       success: res => {
  //         if (res.data.ResultFlag) {
  //           var srect = res.data.ResultData;
  //           var url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appId + "&secret=" + srect;
  //           wx.request({
  //             url: url,
  //             success: res => {
  //               wx.setStorageSync('tokenData', res.data.access_token)// 存入token
  //             }
  //           })
  //         }
  //       },
  //     })
  //     that.data.time = setInterval(() => {
  //       this.getAccessToken()
  //     }, 7200000)
  //   }
  // },
})